package com.hxd.otc.module.wallet.model;

import com.hxd.otc.api.IApi;
import com.hxd.otc.api.IPayApi;
import com.hxd.otc.base.BaseApplication;
import com.hxd.otc.base.BaseBean;
import com.hxd.otc.common.HostType;
import com.hxd.otc.common.UserHelper;
import com.hxd.otc.module.wallet.bean.BasePayBean;
import com.hxd.otc.module.wallet.bean.CreateOrderBean;
import com.hxd.otc.module.wallet.bean.CreateOrderResultBean;
import com.hxd.otc.module.wallet.bean.PayInfoBean;
import com.hxd.otc.module.wallet.bean.PayOrderResultBean;
import com.hxd.otc.net.BaseObserver;
import com.hxd.otc.net.RetrofitManager;
import com.hxd.otc.utils.EncryptUtils;
import com.hxd.otc.utils.GsonUtil;
import com.hxd.otc.utils.PayUtils;
import com.uber.autodispose.AutoDisposeConverter;

import java.util.TreeMap;

import io.reactivex.android.schedulers.AndroidSchedulers;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Retrofit;

/**
 * Created by lzy on 2018/6/14 0014.
 * description ${支付接口model}
 */
public class PayModel {

    private static Retrofit mRetrofit;

    private static class SingletonHolder {
        private static final PayModel instance = new PayModel();
    }

    public static PayModel getInstance() {
        initPayRetrofit();
        return PayModel.SingletonHolder.instance;
    }


    public void createOrder(BasePayBean basePayBean, BaseObserver<CreateOrderResultBean> observer, AutoDisposeConverter<BaseBean<CreateOrderResultBean>> converter) {

        CreateOrderBean createOrderBean = new CreateOrderBean("AEC", 100, "zyzd" + System.currentTimeMillis(), "斩阳科技", "https://www.baidu.com", "https://www.baidu.com");
        TreeMap<String, String> map = PayUtils.generatePayData(basePayBean.getApiKey(), basePayBean.getSecretKey(), basePayBean.getDataKey(), createOrderBean);
        RequestBody requestBody = RequestBody.create(MediaType.parse(IApi.REQUEST_JSON_TYPE),
                GsonUtil.toJson(map));

        getPayApi().createOrder(requestBody)
                .observeOn(AndroidSchedulers.mainThread())
                .as(converter)
                .subscribe(observer);
    }


    public void payOrder(BasePayBean basePayBean, String payPassword, BaseObserver<PayOrderResultBean> observer, AutoDisposeConverter<BaseBean<PayOrderResultBean>> converter) {

        BasePayBean requestBasePayBean = new BasePayBean();
        requestBasePayBean.setApiKey(basePayBean.getApiKey());
        requestBasePayBean.setSecretKey(basePayBean.getSecretKey());
        requestBasePayBean.setDataKey(basePayBean.getDataKey());

        PayInfoBean payInfoBean = basePayBean.getPayInfoBean();
        PayInfoBean requestPayInfoBean = requestBasePayBean.getPayInfoBean();

        requestPayInfoBean.setPayUserId(UserHelper.getId());
        requestPayInfoBean.setCoinType(payInfoBean.getCoinType());
        requestPayInfoBean.setOrderId(payInfoBean.getOrderId());
        requestPayInfoBean.setPreId(payInfoBean.getPreId());
        requestPayInfoBean.setAmountCny(payInfoBean.getAmountCny());
        requestPayInfoBean.setAmountCoin(payInfoBean.getAmountCoin());
        requestPayInfoBean.setPayPassword(EncryptUtils.getSHA256(payPassword));

        RequestBody requestBody = RequestBody.create(MediaType.parse(IApi.REQUEST_JSON_TYPE),
                GsonUtil.toJson(PayUtils.generatePayData(requestBasePayBean)));

        getPayApi().payOrder(requestBody)
                .observeOn(AndroidSchedulers.mainThread())
                .as(converter)
                .subscribe(observer);
    }

    public void queryOrder(BasePayBean basePayBean, BaseObserver<PayOrderResultBean> observer, AutoDisposeConverter<BaseBean<PayOrderResultBean>> converter) {

        BasePayBean requestBasePayBean = new BasePayBean();
        requestBasePayBean.setApiKey(basePayBean.getApiKey());
        requestBasePayBean.setSecretKey(basePayBean.getSecretKey());
        requestBasePayBean.setDataKey(basePayBean.getDataKey());

        PayInfoBean payInfoBean = basePayBean.getPayInfoBean();
        PayInfoBean requestPayInfoBean = requestBasePayBean.getPayInfoBean();

        requestPayInfoBean.setCoinType(payInfoBean.getCoinType());
        requestPayInfoBean.setOrderId(payInfoBean.getOrderId());
        requestPayInfoBean.setPreId(payInfoBean.getPreId());

        RequestBody requestBody = RequestBody.create(MediaType.parse(IApi.REQUEST_JSON_TYPE),
                GsonUtil.toJson(PayUtils.generatePayData(requestBasePayBean)));

        getPayApi().queryOrder(requestBody)
                .observeOn(AndroidSchedulers.mainThread())
                .as(converter)
                .subscribe(observer);
    }

    public void innerQueryOrder(String preId, BaseObserver<PayOrderResultBean> observer, AutoDisposeConverter<BaseBean<PayOrderResultBean>> converter) {
        getPayApi().innerQueryOrder(preId)
                .observeOn(AndroidSchedulers.mainThread())
                .as(converter)
                .subscribe(observer);
    }

    public void innerPayOrder(String preId, String payPassWord, BaseObserver<PayOrderResultBean> observer, AutoDisposeConverter<BaseBean<PayOrderResultBean>> converter) {
        getPayApi().innerPayOrder(preId,UserHelper.getId(), EncryptUtils.getSHA256(payPassWord))
                .observeOn(AndroidSchedulers.mainThread())
                .as(converter)
                .subscribe(observer);
    }

    private IPayApi getPayApi() {
        return mRetrofit.create(IPayApi.class);
    }

    public static void initPayRetrofit() {
        if (mRetrofit != null) return;
        if (BaseApplication.getServerHost() == HostType.HOST_STAND_01) {//开发
            mRetrofit = RetrofitManager.getInstance(HostType.HOST_REMOTE_DEVELOP);
        } else if (BaseApplication.getServerHost() == HostType.HOST_TEST) {//内部测试
            mRetrofit = RetrofitManager.getInstance(HostType.HOST_REMOTE_TEST);
        } else if (BaseApplication.getServerHost() == HostType.HOST_FORMAL_TEST) {//客户测试
            mRetrofit = RetrofitManager.getInstance(HostType.HOST_REMOTE_FORMAL_TEST);
        } else { //客户正式
            mRetrofit = RetrofitManager.getInstance(HostType.HOST_REMOTE_FORMAL);
        }
    }
}
